/*
  * Mirante Tecnologia
  * Copyright 2010, Mirante Informatica LTDA, 
  * and individual contributors as indicated by the @authors tag
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
package com.miranteinfo.seam.hibernate.criterion;

import org.hibernate.criterion.Example;
import org.hibernate.type.Type;

/**
 * Classe para extender a funcionalidade da busca por exemplo do hibernate, permitindo excluir, 
 * da busca por exemplo, atributos em branco.
 * 
 * @author lucas lins
 *
 */
public class HibernateExample extends Example{
	
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// ATTRIBUTES
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	private static final long serialVersionUID = 1L;
	
	private static final PropertySelector NOT_NULL = new NotNullPropertySelector();
	private static final PropertySelector NOT_EMPTY = new NotEmptyPropertySelector();
	

	static final class NotNullPropertySelector implements PropertySelector {
		private static final long serialVersionUID = 1L;

		public boolean include(Object object, String propertyName, Type type) {
			return object!=null;
		}
	}
	
	static final class NotEmptyPropertySelector implements PropertySelector {
		private static final long serialVersionUID = 1L;
		
		public boolean include(Object object, String propertyName, Type type) {
			return object!=null && (
					!(object instanceof String) || !((String)object).trim().equals("")
				);
		}
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// CONSTRUCTOR
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	protected HibernateExample(Object entity, PropertySelector selector) {
		super(entity, selector);
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// PUBLIC METHODS
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	public HibernateExample excludeEmpty(){
		setPropertySelector(NOT_EMPTY);
		return this;
	}
	
	public static HibernateExample create(Object entity) {
		if(entity == null) throw new NullPointerException("null example");
		return new HibernateExample(entity, NOT_NULL);
	}

}
